{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 波士顿房价预测案例——线性回归分析\n",
    "\n",
    "在这个案例中，我们将利用波士顿郊区的房屋信息数据训练和测试一个模型，并对模型的性能和预测能力进行测试。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1、导入必要的工具包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np  # 矩阵操作\n",
    "import pandas as pd # SQL数据处理\n",
    "\n",
    "from sklearn.metrics import r2_score  #评价回归预测模型的性能\n",
    "\n",
    "import matplotlib.pyplot as plt   #画图\n",
    "import seaborn as sns\n",
    "\n",
    "# 图形出现在Notebook里而不是新窗口\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. 读取数据\n",
    "做完特征工程后的数据，请先运行2_FE_BostonHousePrice.ipynb，得到文件FE_boston_housing.csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CRIM</th>\n",
       "      <th>ZN</th>\n",
       "      <th>INDUS</th>\n",
       "      <th>CHAS</th>\n",
       "      <th>NOX</th>\n",
       "      <th>RM</th>\n",
       "      <th>AGE</th>\n",
       "      <th>DIS</th>\n",
       "      <th>TAX</th>\n",
       "      <th>PTRATIO</th>\n",
       "      <th>...</th>\n",
       "      <th>RAD_2</th>\n",
       "      <th>RAD_3</th>\n",
       "      <th>RAD_4</th>\n",
       "      <th>RAD_5</th>\n",
       "      <th>RAD_6</th>\n",
       "      <th>RAD_7</th>\n",
       "      <th>RAD_8</th>\n",
       "      <th>RAD_24</th>\n",
       "      <th>MEDV</th>\n",
       "      <th>log_MEDV</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.18</td>\n",
       "      <td>0.067815</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.314815</td>\n",
       "      <td>0.577505</td>\n",
       "      <td>0.641607</td>\n",
       "      <td>0.269203</td>\n",
       "      <td>0.208015</td>\n",
       "      <td>0.3</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>24.0</td>\n",
       "      <td>3.218876</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>0.000236</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.242302</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.172840</td>\n",
       "      <td>0.547998</td>\n",
       "      <td>0.782698</td>\n",
       "      <td>0.348962</td>\n",
       "      <td>0.104962</td>\n",
       "      <td>0.5</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>21.6</td>\n",
       "      <td>3.117950</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>0.000236</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.242302</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.172840</td>\n",
       "      <td>0.694386</td>\n",
       "      <td>0.599382</td>\n",
       "      <td>0.348962</td>\n",
       "      <td>0.104962</td>\n",
       "      <td>0.5</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>34.7</td>\n",
       "      <td>3.575151</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>0.000293</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.063050</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.150206</td>\n",
       "      <td>0.658555</td>\n",
       "      <td>0.441813</td>\n",
       "      <td>0.448545</td>\n",
       "      <td>0.066794</td>\n",
       "      <td>0.6</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>33.4</td>\n",
       "      <td>3.538057</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>0.000705</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.063050</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.150206</td>\n",
       "      <td>0.687105</td>\n",
       "      <td>0.528321</td>\n",
       "      <td>0.448545</td>\n",
       "      <td>0.066794</td>\n",
       "      <td>0.6</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>36.2</td>\n",
       "      <td>3.616309</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 23 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       CRIM    ZN     INDUS  CHAS       NOX        RM       AGE       DIS  \\\n",
       "0  0.000000  0.18  0.067815   0.0  0.314815  0.577505  0.641607  0.269203   \n",
       "1  0.000236  0.00  0.242302   0.0  0.172840  0.547998  0.782698  0.348962   \n",
       "2  0.000236  0.00  0.242302   0.0  0.172840  0.694386  0.599382  0.348962   \n",
       "3  0.000293  0.00  0.063050   0.0  0.150206  0.658555  0.441813  0.448545   \n",
       "4  0.000705  0.00  0.063050   0.0  0.150206  0.687105  0.528321  0.448545   \n",
       "\n",
       "        TAX  PTRATIO  ...  RAD_2  RAD_3  RAD_4  RAD_5  RAD_6  RAD_7  RAD_8  \\\n",
       "0  0.208015      0.3  ...      0      0      0      0      0      0      0   \n",
       "1  0.104962      0.5  ...      1      0      0      0      0      0      0   \n",
       "2  0.104962      0.5  ...      1      0      0      0      0      0      0   \n",
       "3  0.066794      0.6  ...      0      1      0      0      0      0      0   \n",
       "4  0.066794      0.6  ...      0      1      0      0      0      0      0   \n",
       "\n",
       "   RAD_24  MEDV  log_MEDV  \n",
       "0       0  24.0  3.218876  \n",
       "1       0  21.6  3.117950  \n",
       "2       0  34.7  3.575151  \n",
       "3       0  33.4  3.538057  \n",
       "4       0  36.2  3.616309  \n",
       "\n",
       "[5 rows x 23 columns]"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# path to where the data lies\n",
    "#dpath = 'E:\\jupyter book data'\n",
    "df = pd.read_csv(\"FE_boston_housing.csv\")\n",
    "\n",
    "#通过观察前5行，了解数据每列（特征）的概况\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  数据基本信息\n",
    "样本数目、特征维数\n",
    "每个特征的类型、空值样本的数目、数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "#df.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数据准备"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 从原始数据中分离输入特征x和输出y\n",
    "y = df[\"MEDV\"]\n",
    "\n",
    "X = df.drop([\"MEDV\", \"log_MEDV\"], axis = 1)\n",
    "\n",
    "#特征名称，用于后续显示权重系数对应的特征\n",
    "feat_names = X.columns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当数据量比较大时，可用train_test_split从训练集中分出一部分做校验集；\n",
    "样本数目较少时，建议用交叉验证。\n",
    "在线性回归中，留一交叉验证有简便计算方式。\n",
    "\n",
    "下面将训练数据分割成训练集和测试集，只是让大家对模型的训练误差、校验集上的测试误差估计、和测试集上的测试误差做个比较。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(404, 21)"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#将数据分割训练数据与测试数据\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "# 随机采样20%的数据构建测试样本，其余作为训练样本\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=33, test_size=0.2)\n",
    "X_train.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3、确定模型类型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.1 尝试缺省参数的线性回归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>columns</th>\n",
       "      <th>coef</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>5</td>\n",
       "      <td>RM</td>\n",
       "      <td>20.356954</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>ZN</td>\n",
       "      <td>5.815739</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>20</td>\n",
       "      <td>RAD_24</td>\n",
       "      <td>4.644938</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>10</td>\n",
       "      <td>B</td>\n",
       "      <td>3.550457</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>CHAS</td>\n",
       "      <td>2.684704</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>18</td>\n",
       "      <td>RAD_7</td>\n",
       "      <td>1.694896</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>19</td>\n",
       "      <td>RAD_8</td>\n",
       "      <td>1.353203</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>14</td>\n",
       "      <td>RAD_3</td>\n",
       "      <td>1.235936</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>INDUS</td>\n",
       "      <td>0.621810</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>6</td>\n",
       "      <td>AGE</td>\n",
       "      <td>-0.055280</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>15</td>\n",
       "      <td>RAD_4</td>\n",
       "      <td>-0.391399</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>16</td>\n",
       "      <td>RAD_5</td>\n",
       "      <td>-0.518265</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>13</td>\n",
       "      <td>RAD_2</td>\n",
       "      <td>-1.843667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>17</td>\n",
       "      <td>RAD_6</td>\n",
       "      <td>-2.508106</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>12</td>\n",
       "      <td>RAD_1</td>\n",
       "      <td>-3.667536</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>8</td>\n",
       "      <td>TAX</td>\n",
       "      <td>-5.109656</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>NOX</td>\n",
       "      <td>-6.819892</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>9</td>\n",
       "      <td>PTRATIO</td>\n",
       "      <td>-8.455428</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>CRIM</td>\n",
       "      <td>-9.984341</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>7</td>\n",
       "      <td>DIS</td>\n",
       "      <td>-17.381223</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>11</td>\n",
       "      <td>LSTAT</td>\n",
       "      <td>-21.450356</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    columns       coef\n",
       "5        RM  20.356954\n",
       "1        ZN   5.815739\n",
       "20   RAD_24   4.644938\n",
       "10        B   3.550457\n",
       "3      CHAS   2.684704\n",
       "18    RAD_7   1.694896\n",
       "19    RAD_8   1.353203\n",
       "14    RAD_3   1.235936\n",
       "2     INDUS   0.621810\n",
       "6       AGE  -0.055280\n",
       "15    RAD_4  -0.391399\n",
       "16    RAD_5  -0.518265\n",
       "13    RAD_2  -1.843667\n",
       "17    RAD_6  -2.508106\n",
       "12    RAD_1  -3.667536\n",
       "8       TAX  -5.109656\n",
       "4       NOX  -6.819892\n",
       "9   PTRATIO  -8.455428\n",
       "0      CRIM  -9.984341\n",
       "7       DIS -17.381223\n",
       "11    LSTAT -21.450356"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 线性回归\n",
    "#class sklearn.linear_model.LinearRegression(fit_intercept=True, normalize=False, copy_X=True, n_jobs=1)\n",
    "from sklearn.linear_model import LinearRegression\n",
    "\n",
    "# 1.使用默认配置初始化学习器实例\n",
    "lr = LinearRegression()\n",
    "\n",
    "# 2.用训练数据训练模型参数\n",
    "lr.fit(X_train, y_train)\n",
    "\n",
    "# 3. 用训练好的模型对测试集进行预测\n",
    "y_test_pred_lr = lr.predict(X_test)\n",
    "y_train_pred_lr = lr.predict(X_train)\n",
    "\n",
    "\n",
    "# 看看各特征的权重系数，系数的绝对值大小可视为该特征的重要性\n",
    "fs = pd.DataFrame({\"columns\":list(feat_names), \"coef\":list((lr.coef_.T))})\n",
    "fs.sort_values(by=['coef'],ascending=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3.1.1 模型评价"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The r2 score of LinearRegression on test is 0.6939789810509476\n",
      "The r2 score of LinearRegression on train is 0.7549146436868177\n"
     ]
    }
   ],
   "source": [
    "# 使用r2_score评价模型在测试集和训练集上的性能，并输出评估结果\n",
    "#测试集\n",
    "print (\"The r2 score of LinearRegression on test is\", r2_score(y_test, y_test_pred_lr))\n",
    "#训练集\n",
    "print (\"The r2 score of LinearRegression on train is\", r2_score(y_train, y_train_pred_lr))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAeUAAAFsCAYAAADyj6FyAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0R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NrULrJ4EDqV2mNRDU5J65OFrSZIKYU9ZkqRC7NaH4O+///45YcKE3blJSZKK0tHR8XRmju5u2m4N5QkTJrB48eLduUlJkooSET0+rc7D15IkFcJQliSpEIayJEmF2K3nlCVJ3duwYQMrV67kpZd84mezGD58OOPHj6elpaXXyxjKklSAlStXsvfeezNhwgQi6vXlYGqUzGTdunWsXLmSiRMn9no5D19LUgFeeukl9ttvPwO5SUQE++233y4f+TCUJakQBnJz6cv+NJQlSSqE55QlqUDt7bt/fUOGDOGoo45i48aNTJw4ke985zvsu+++u7ytiy66iI9+9KNMmjRpm/E33ngjixcv5qtf/eourxOgtbWV9evX92reGTNmcM0119DW1rZ13OLFi7n55pu57rrr+rT93cGesiQJgFe/+tUsWbKEhx9+mFGjRnH99df3aT3f+MY3tgvkErS1tQ14IG/atKlfyxvKkqTtTJ8+nVWrVm0dvvrqqzn22GM5+uijufLKKwF48cUXmTVrFpMnT+bII49k3rx5QK2XuuWRyt/+9rc57LDDOOmkk7jvvvu2ru+CCy7gzjvv3Drc2toKwPr165k5cybHHHMMRx11FPPnz9+uttWrV3PiiScyZcoUjjzySH72s5/1qk0LFy7ktNNOA6C9vZ0LL7yQGTNmcPDBB28T1rfccgvHHXccU6ZM4f3vf//WoL300ktpa2vjiCOO2PoeQO0R0ldddRVvfetbueOOO3pVS088fC1J2samTZtYsGABc+fOBeDuu+9m2bJlPPDAA2Qmp59+OosWLWLt2rUceOCB/PjHPwbgueee22Y9q1ev5sorr6Sjo4ORI0fytre9jalTp+5w28OHD+cHP/gB++yzD08//TTTpk3j9NNP3+aiqe9+97uccsopfPrTn2bTpk388Y9/7FM7f/3rX3PPPffwwgsv8MY3vpFLL72U5cuXM2/ePO677z5aWlr4wAc+wK233sr555/P5z//eUaNGsWmTZuYOXMmDz30EEcfffTWuu+9994+1dGZoSxJAuBPf/oTU6ZM4YknnuDNb34zJ598MlAL5bvvvntroK5fv55ly5Zxwgkn8LGPfYxPfvKTnHbaaZxwwgnbrO/+++9n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Dr7UEQ8VB1S22MPJ3XRzPurswTujoiO6tG0zeqAzFwNtV+GwJgG1zNQmu6zGBETgKnA/TTpfuzSRmjAftyjQzki/jkiHu7m3456Ur16PGiJdtLerwGHAFOA1cDfNbTY+tlj99cuOj4zj6H2rWsfrA6Las/UdJ/FiGgF/gn4cGY+3+h6BkI3bWzIfuzPs68bLjPf0YfF9tjHg/a2vRHxj8CPBric3WWP3V+7IjOfrH4+FRE/oHbYflFjqxoQayJibGaurs7lPdXoguotM9dsed0Mn8WIaKEWVrdm5ver0U21H7trY6P24x7dU+6ju4CzImJYREwEDgUeaHBN/VZ9MLZ4N/BwT/PuYZr+ca4RMaK6wISIGAG8k+bZf13dBcypXs8B5jewlgHRTJ/FiAjgm8DSzLy206Sm2Y89tbFR+7GZr75+N/D3wGjgWWBJZp5STfs0cCGwkdqhip80rNA6iYjvUDvMksATwPu3nPPZ01W3InyZVx7n+vkGl1RXEXEw8INqcCjw3WZoY0R8D5hB7Wvw1gBXAj8EbgcOAv4TeG9m7rEXSvXQxhk0yWcxIt4K/Az4FbW7WQCuoHbOtSn24w7aeDYN2I9NG8qSJO1pBuPha0mSimQoS5JUCENZkqRCGMqSJBXCUJYkqRCGsiRJhTCUJUkqxP8HPaCQS9SdSjkAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 504x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#在训练集上观察预测残差的分布，看是否符合模型假设：噪声为0均值的高斯噪声\n",
    "f, ax = plt.subplots(figsize=(7, 5)) \n",
    "f.tight_layout() \n",
    "ax.hist(y_train - y_train_pred_lr, bins=40, label='Residuals Linear', color='b', alpha=.5); \n",
    "ax.set_title(\"Histogram of Residuals\") \n",
    "ax.legend(loc='best');"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhE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wHGqOr/cshjxYRaMYRixYRaMYRixYRaMYRixYQrGMIzYMAVjGEZsmIIxDCM2TMEYhhEb/x/AmfOWbOY6XwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 288x216 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#还可以观察预测值与真值的散点图\n",
    "plt.figure(figsize=(4, 3))\n",
    "plt.scatter(y_train, y_train_pred_lr)\n",
    "plt.plot([-3, 3], [-3, 3], '--k')   #数据已经标准化，3倍标准差即可\n",
    "plt.axis('tight')\n",
    "plt.xlabel('True price')\n",
    "plt.ylabel('Predicted price')\n",
    "plt.tight_layout()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2 正则化的线性回归（L2正则 --> 岭回归）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The r2 score of RidgeCV on test is 0.6961684813069751\n",
      "The r2 score of RidgeCV on train is 0.7548524440445545\n"
     ]
    }
   ],
   "source": [
    "#岭回归／L2正则\n",
    "#class sklearn.linear_model.RidgeCV(alphas=(0.1, 1.0, 10.0), fit_intercept=True, \n",
    "#                                  normalize=False, scoring=None, cv=None, gcv_mode=None, \n",
    "#                                  store_cv_values=False)\n",
    "from sklearn.linear_model import  RidgeCV\n",
    "\n",
    "#1. 设置超参数（正则参数）范围\n",
    "alphas = [ 0.01, 0.1, 1, 10,100]\n",
    "#n_alphas = 20\n",
    "#alphas = np.logspace(-5,2,n_alphas)\n",
    "\n",
    "#2. 生成一个RidgeCV实例\n",
    "ridge = RidgeCV(alphas=alphas, store_cv_values=True)  \n",
    "\n",
    "#3. 模型训练\n",
    "ridge.fit(X_train, y_train)    \n",
    "\n",
    "#4. 预测\n",
    "y_test_pred_ridge = ridge.predict(X_test)\n",
    "y_train_pred_ridge = ridge.predict(X_train)\n",
    "\n",
    "\n",
    "# 评估，使用r2_score评价模型在测试集和训练集上的性能\n",
    "print (\"The r2 score of RidgeCV on test is\", r2_score(y_test, y_test_pred_ridge))\n",
    "print (\"The r2 score of RidgeCV on train is\", r2_score(y_train, y_train_pred_ridge))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 可视化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX4AAAEGCAYAAABiq/5QAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nO3dd5xcdb3/8dcnPYQUUgkpJKSSYBpLQKKYAlwEDCDdhormIqLh51XB8rtEwetVkGIQ/EVRUFF6JIQW0kCEAJvee98ku+m97M7n98c5k0yW2c1sds+Unffz8djHzpz62ZPNez77nTPnmLsjIiL5o06mCxARkfRS8IuI5BkFv4hInlHwi4jkGQW/iEieqZfpAlLRunVr79KlS6bLEBHJKTNnztzq7m3KT8+J4O/SpQuFhYWZLkNEJKeY2dpk0zXUIyKSZxT8IiJ5RsEvIpJnFPwiInlGwS8ikmcU/CIieUbBLyKSZxT8IiJZ6OCRMsZMWMiGHftrfNsKfhGRLPT0B+t48r01bNhxoMa3reAXEckyB4+U8fu3V/LJs1pxwVmtanz7Cn4RkSzz9w/WUbLnEKMv7hHJ9hX8IiJZ5OCRMh5/eyUXnNUykm4fFPwiIlnlHx+G3f6InpHtQ8EvIpIlDh4p4/HpQbf/yW7RdPug4BcRyRr/+HAdxRF3+xBx8JvZGjObb2ZzzKwwnDbGzDaG0+aY2eVR1iAikgvi3f75XaPt9iE9N2IZ5u5by017yN0fSMO+RURywjNht//ITQMj35eGekREMix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s3+Fy88yswsTlv9nWINIjVLwS774PDAA6A9cDNwfXs7g80AX4BPAN4BPJqwzBJiZ8Pwn7l4A9AM+Y2b9kuynCTDL3QcBbwP3JMyr5+6DgTsTphcDl4TL3wj8NmH5QuDTVf9RRSqXkxdpEzkJnyK4SmUZsMXM3gbOC6c/7+4xYLOZTUtYpz1QkvD8hvBS2fXCeX0IPmafKAY8Gz7+G5B4wa3445kELzYA9YFHzWwAUAb0TFi+mODSGSI1SsEv+aKim6JUdrOUAwTX4MHMugLfB85z9x1m9mR83gkkXhPlUPi9jGP/9/4PwTWS+hP8BX4wYflGYQ0iNUpDPZIv3gFuNLO64bj7RQQXU3uX4EYldcysHcGt9+IWA93Dx82AfcCucLnPVrCfOgRX5wT4Qrj9yjQHNoV/cXyZ4HaKcT3JjquzSi2jjl/yxXiC8fu5BF34D919s5m9CIwgCNhlBHc+2hWu8yrBC8Fkd59rZrMJrty4Cvh3BfvZB/Q1s5nhdm48QV2PAS+a2fUEV8/clzBvWFiDSI3S1Tkl75nZqR7ccakVwV8BQ8IXhcYEYTwkfG8glW3tdfdTa6iud4Cr3H1HTWxPJE4dvwhMNH04oZEAAABGSURBVLMWQAPgXnffDODuB8zsHoJ7m65LZ0HhcNSDCn2Jgjp+EZE8ozd3RUTyjIJfRCTPKPhFRPKMgl9EJM8o+EVE8sz/B0gfzPX1OixnAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "alpha is: 0.1\n"
     ]
    }
   ],
   "source": [
    "mse_mean = np.mean(ridge.cv_values_, axis = 0)\n",
    "plt.plot(np.log10(alphas), mse_mean.reshape(len(alphas),1)) \n",
    "\n",
    "#这是为了标出最佳参数的位置，不是必须\n",
    "#plt.plot(np.log10(ridge.alpha_)*np.ones(3), [0.28, 0.29, 0.30])\n",
    "\n",
    "plt.xlabel('log(alpha)')\n",
    "plt.ylabel('mse')\n",
    "plt.show()\n",
    "\n",
    "print ('alpha is:', ridge.alpha_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>columns</th>\n",
       "      <th>coef_lr</th>\n",
       "      <th>coef_ridge</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>5</td>\n",
       "      <td>RM</td>\n",
       "      <td>20.356954</td>\n",
       "      <td>20.209673</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>ZN</td>\n",
       "      <td>5.815739</td>\n",
       "      <td>5.668449</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>20</td>\n",
       "      <td>RAD_24</td>\n",
       "      <td>4.644938</td>\n",
       "      <td>4.495999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>10</td>\n",
       "      <td>B</td>\n",
       "      <td>3.550457</td>\n",
       "      <td>3.569715</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>CHAS</td>\n",
       "      <td>2.684704</td>\n",
       "      <td>2.703375</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>18</td>\n",
       "      <td>RAD_7</td>\n",
       "      <td>1.694896</td>\n",
       "      <td>1.652060</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>19</td>\n",
       "      <td>RAD_8</td>\n",
       "      <td>1.353203</td>\n",
       "      <td>1.369027</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>14</td>\n",
       "      <td>RAD_3</td>\n",
       "      <td>1.235936</td>\n",
       "      <td>1.276021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>INDUS</td>\n",
       "      <td>0.621810</td>\n",
       "      <td>0.517086</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>6</td>\n",
       "      <td>AGE</td>\n",
       "      <td>-0.055280</td>\n",
       "      <td>-0.048454</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>15</td>\n",
       "      <td>RAD_4</td>\n",
       "      <td>-0.391399</td>\n",
       "      <td>-0.389052</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>16</td>\n",
       "      <td>RAD_5</td>\n",
       "      <td>-0.518265</td>\n",
       "      <td>-0.503234</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>13</td>\n",
       "      <td>RAD_2</td>\n",
       "      <td>-1.843667</td>\n",
       "      <td>-1.756881</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>17</td>\n",
       "      <td>RAD_6</td>\n",
       "      <td>-2.508106</td>\n",
       "      <td>-2.495217</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>12</td>\n",
       "      <td>RAD_1</td>\n",
       "      <td>-3.667536</td>\n",
       "      <td>-3.648724</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>8</td>\n",
       "      <td>TAX</td>\n",
       "      <td>-5.109656</td>\n",
       "      <td>-4.958641</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>NOX</td>\n",
       "      <td>-6.819892</td>\n",
       "      <td>-6.565940</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>9</td>\n",
       "      <td>PTRATIO</td>\n",
       "      <td>-8.455428</td>\n",
       "      <td>-8.431607</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>CRIM</td>\n",
       "      <td>-9.984341</td>\n",
       "      <td>-9.567877</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>7</td>\n",
       "      <td>DIS</td>\n",
       "      <td>-17.381223</td>\n",
       "      <td>-16.868997</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>11</td>\n",
       "      <td>LSTAT</td>\n",
       "      <td>-21.450356</td>\n",
       "      <td>-21.357121</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    columns    coef_lr  coef_ridge\n",
       "5        RM  20.356954   20.209673\n",
       "1        ZN   5.815739    5.668449\n",
       "20   RAD_24   4.644938    4.495999\n",
       "10        B   3.550457    3.569715\n",
       "3      CHAS   2.684704    2.703375\n",
       "18    RAD_7   1.694896    1.652060\n",
       "19    RAD_8   1.353203    1.369027\n",
       "14    RAD_3   1.235936    1.276021\n",
       "2     INDUS   0.621810    0.517086\n",
       "6       AGE  -0.055280   -0.048454\n",
       "15    RAD_4  -0.391399   -0.389052\n",
       "16    RAD_5  -0.518265   -0.503234\n",
       "13    RAD_2  -1.843667   -1.756881\n",
       "17    RAD_6  -2.508106   -2.495217\n",
       "12    RAD_1  -3.667536   -3.648724\n",
       "8       TAX  -5.109656   -4.958641\n",
       "4       NOX  -6.819892   -6.565940\n",
       "9   PTRATIO  -8.455428   -8.431607\n",
       "0      CRIM  -9.984341   -9.567877\n",
       "7       DIS -17.381223  -16.868997\n",
       "11    LSTAT -21.450356  -21.357121"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 看看各特征的权重系数，系数的绝对值大小可视为该特征的重要性\n",
    "fs = pd.DataFrame({\"columns\":list(feat_names), \"coef_lr\":list((lr.coef_.T)), \"coef_ridge\":list((ridge.coef_.T))})\n",
    "fs.sort_values(by=['coef_lr'],ascending=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.3 正则化的线性回归（L1正则 --> Lasso）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\users\\lenovo\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\sklearn\\model_selection\\_split.py:1978: FutureWarning: The default value of cv will change from 3 to 5 in version 0.22. Specify it explicitly to silence this warning.\n",
      "  warnings.warn(CV_WARNING, FutureWarning)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The r2 score of LassoCV on test is 0.6945099371682095\n",
      "The r2 score of LassoCV on train is 0.7548861511225583\n"
     ]
    }
   ],
   "source": [
    "#### Lasso／L1正则\n",
    "# class sklearn.linear_model.LassoCV(eps=0.001, n_alphas=100, alphas=None, fit_intercept=True, \n",
    "#                                    normalize=False, precompute=’auto’, max_iter=1000, \n",
    "#                                    tol=0.0001, copy_X=True, cv=None, verbose=False, n_jobs=1,\n",
    "#                                    positive=False, random_state=None, selection=’cyclic’)\n",
    "from sklearn.linear_model import LassoCV\n",
    "\n",
    "#1. 设置超参数搜索范围\n",
    "#alphas = [ 0.01, 0.1, 1, 10,100]\n",
    "\n",
    "#2. 生成学习器实例\n",
    "#lasso = LassoCV(alphas=alphas)\n",
    "\n",
    "#1. 设置超参数搜索范围\n",
    "#Lasso可以自动确定最大的alpha，所以另一种设置alpha的方式是设置最小的alpha值（eps） 和 超参数的数目（n_alphas），\n",
    "#然后LassoCV对最小值和最大值之间在log域上均匀取值n_alphas个\n",
    "# np.logspace(np.log10(alpha_max * eps), np.log10(alpha_max),num=n_alphas)[::-1]\n",
    "\n",
    "#2 生成LassoCV实例（默认超参数搜索范围）\n",
    "lasso = LassoCV()  \n",
    "\n",
    "#3. 训练（内含CV）\n",
    "lasso.fit(X_train, y_train)  \n",
    "\n",
    "#4. 测试\n",
    "y_test_pred_lasso = lasso.predict(X_test)\n",
    "y_train_pred_lasso = lasso.predict(X_train)\n",
    "\n",
    "\n",
    "# 评估，使用r2_score评价模型在测试集和训练集上的性能\n",
    "print (\"The r2 score of LassoCV on test is\", r2_score(y_test, y_test_pred_lasso))\n",
    "print (\"The r2 score of LassoCV on train is\", r2_score(y_train, y_train_pred_lasso))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX4AAAEGCAYAAABiq/5QAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nO3deXhc9X3v8fd3tFqLLVuS5UXYljeMzWYQ+5IEMIVsQAJNmqR1E1K3z81tm9A+Cbn0lqZpn1vStGnStOmlydO6SVq2wAWyAMZsTQLENmC875ssW5IlWfs+3/vHHBnhSPYYdObMaD6v59FzlvnNnO+ZsT46/s05v2PujoiIZI9Y1AWIiEhqKfhFRLKMgl9EJMso+EVEsoyCX0Qky+RGXUAyKioqfN68eVGXISKSUTZs2HDM3StPXp8RwT9v3jzWr18fdRkiIhnFzA6Mtl5dPSIiWUbBLyKSZRT8IiJZRsEvIpJlFPwiIllGwS8ikmUU/CIiWUbBLyKShupau7nvqe00dvSO+2sr+EVE0tDD6+v4lxf30D8YH/fXVvCLiKSZobjz8PpDXLOokuqpReP++gp+EZE089+7mqhv6+Xjl5wVyusr+EVE0syD6w4xrTifG86pCuX1FfwiImnkWGcfa7Y28NGLZpOfG05EK/hFRNLIo6/VMRh3PhZSNw8o+EVE0oa788C6Q1w8dyoLp5eGth0Fv4hImthwoJW9TV2hHu2Dgl9EJG2s2dpAXo7xgfNmhrodBb+ISJp4eW8zy8+aSnFBuDdHVPCLiKSB9t4BNh9u4/IF5aFvS8EvIpIG1u1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K1jOzEnfvNLNyEv8LuCr4ozCJRBhfFXw3kMxrdbp7yTjV9RJwiyfuJy0ybnTELwI/NrMyIB/4qrsfBXD3HjO7l8R9Ug+msqCgO+rvFfoSBh3xi4hkGX25KyKSZRT8IiJZRsEvIpJlFPwiIllGwS8ikmX+PwcBbnHRN9rtAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "alpha is: 0.0013884165768061959\n"
     ]
    }
   ],
   "source": [
    "mses = np.mean(lasso.mse_path_, axis = 1)\n",
    "plt.plot(np.log10(lasso.alphas_), mses) \n",
    "#plt.plot(np.log10(lasso.alphas_)*np.ones(3), [0.3, 0.4, 1.0])\n",
    "plt.xlabel('log(alpha)')\n",
    "plt.ylabel('mse')\n",
    "plt.show()    \n",
    "            \n",
    "print ('alpha is:', lasso.alpha_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>coef_lasso</th>\n",
       "      <th>coef_lr</th>\n",
       "      <th>coef_ridge</th>\n",
       "      <th>columns</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>0.502616</td>\n",
       "      <td>0.505543</td>\n",
       "      <td>0.452289</td>\n",
       "      <td>RAD_24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.300666</td>\n",
       "      <td>0.297984</td>\n",
       "      <td>0.301090</td>\n",
       "      <td>RM</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>0.202829</td>\n",
       "      <td>0.184468</td>\n",
       "      <td>0.171709</td>\n",
       "      <td>RAD_7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.141833</td>\n",
       "      <td>0.147403</td>\n",
       "      <td>0.143205</td>\n",
       "      <td>ZN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>0.172528</td>\n",
       "      <td>0.147279</td>\n",
       "      <td>0.140265</td>\n",
       "      <td>RAD_8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>0.174058</td>\n",
       "      <td>0.134516</td>\n",
       "      <td>0.140135</td>\n",
       "      <td>RAD_3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>0.087695</td>\n",
       "      <td>0.088869</td>\n",
       "      <td>0.088272</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.075667</td>\n",
       "      <td>0.074143</td>\n",
       "      <td>0.075931</td>\n",
       "      <td>CHAS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.007140</td>\n",
       "      <td>0.017002</td>\n",
       "      <td>0.010758</td>\n",
       "      <td>INDUS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>-0.001587</td>\n",
       "      <td>-0.001742</td>\n",
       "      <td>-0.004042</td>\n",
       "      <td>AGE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>-0.042599</td>\n",
       "      <td>-0.042286</td>\n",
       "      <td>RAD_4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>-0.009046</td>\n",
       "      <td>-0.056407</td>\n",
       "      <td>-0.053816</td>\n",
       "      <td>RAD_5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.101801</td>\n",
       "      <td>-0.104955</td>\n",
       "      <td>-0.101851</td>\n",
       "      <td>CRIM</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.170409</td>\n",
       "      <td>-0.176803</td>\n",
       "      <td>-0.170743</td>\n",
       "      <td>NOX</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>-0.158660</td>\n",
       "      <td>-0.178692</td>\n",
       "      <td>-0.157054</td>\n",
       "      <td>TAX</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>-0.128783</td>\n",
       "      <td>-0.200660</td>\n",
       "      <td>-0.177225</td>\n",
       "      <td>RAD_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>-0.205211</td>\n",
       "      <td>-0.209666</td>\n",
       "      <td>-0.205903</td>\n",
       "      <td>PTRATIO</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>-0.221099</td>\n",
       "      <td>-0.272976</td>\n",
       "      <td>-0.264839</td>\n",
       "      <td>RAD_6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>-0.355223</td>\n",
       "      <td>-0.361874</td>\n",
       "      <td>-0.357240</td>\n",
       "      <td>DIS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>-0.331377</td>\n",
       "      <td>-0.399165</td>\n",
       "      <td>-0.366233</td>\n",
       "      <td>RAD_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>-0.459030</td>\n",
       "      <td>-0.459577</td>\n",
       "      <td>-0.457144</td>\n",
       "      <td>LSTAT</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    coef_lasso   coef_lr  coef_ridge  columns\n",
       "20    0.502616  0.505543    0.452289   RAD_24\n",
       "5     0.300666  0.297984    0.301090       RM\n",
       "18    0.202829  0.184468    0.171709    RAD_7\n",
       "1     0.141833  0.147403    0.143205       ZN\n",
       "19    0.172528  0.147279    0.140265    RAD_8\n",
       "14    0.174058  0.134516    0.140135    RAD_3\n",
       "10    0.087695  0.088869    0.088272        B\n",
       "3     0.075667  0.074143    0.075931     CHAS\n",
       "2     0.007140  0.017002    0.010758    INDUS\n",
       "6    -0.001587 -0.001742   -0.004042      AGE\n",
       "15    0.000000 -0.042599   -0.042286    RAD_4\n",
       "16   -0.009046 -0.056407   -0.053816    RAD_5\n",
       "0    -0.101801 -0.104955   -0.101851     CRIM\n",
       "4    -0.170409 -0.176803   -0.170743      NOX\n",
       "8    -0.158660 -0.178692   -0.157054      TAX\n",
       "13   -0.128783 -0.200660   -0.177225    RAD_2\n",
       "9    -0.205211 -0.209666   -0.205903  PTRATIO\n",
       "17   -0.221099 -0.272976   -0.264839    RAD_6\n",
       "7    -0.355223 -0.361874   -0.357240      DIS\n",
       "12   -0.331377 -0.399165   -0.366233    RAD_1\n",
       "11   -0.459030 -0.459577   -0.457144    LSTAT"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 看看各特征的权重系数，系数的绝对值大小可视为该特征的重要性\n",
    "fs = pd.DataFrame({\"columns\":list(feat_names), \"coef_lr\":list((lr.coef_.T)), \"coef_ridge\":list((ridge.coef_.T)), \"coef_lasso\":list((lasso.coef_.T))})\n",
    "fs.sort_values(by=['coef_lr'],ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
